package com.waterworld.watch.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.common.bean.AddressBookBean;
import com.waterworld.watch.common.customview.CircleImageView;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.common.util.PhotoUtils;

import java.util.List;

public class SkillContactListAdapter extends RecyclerView.Adapter<SkillContactListAdapter.MyHolder> {

    private Context mContext;
    private List<AddressBookBean> mData;
    private onContactClickListener onContactClickListener;

    public SkillContactListAdapter(Context mContext, List<AddressBookBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }


    public interface onContactClickListener {
        void contactClick(View view,int position);
    }

    public void setOnContactClickListener(SkillContactListAdapter.onContactClickListener onContactClickListener) {
        this.onContactClickListener = onContactClickListener;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ConstraintLayout parent;
        CircleImageView contactAvatar;
        TextView contactName;
        TextView contactPhone;

        MyHolder(@NonNull View itemView) {
            super(itemView);
            parent = itemView.findViewById(R.id.parent);
            contactAvatar = itemView.findViewById(R.id.iv_contact_avatar);
            contactName = itemView.findViewById(R.id.tv_contact_name);
            contactPhone = itemView.findViewById(R.id.tv_contact_phone);
        }
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.item_contact, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder myHolder, int position) {
        if (mData.get(position).getHead() == null){
            //默认头像
            Glide.with(mContext).load(R.drawable.sys_parent_01).into(myHolder.contactAvatar);
        } else if (mData.get(position).getHead().startsWith("sys_")){
            Glide.with(mContext).load(PhotoUtils.getResource(mData.get(position).getHead().substring(0,13))).into(myHolder.contactAvatar);
        } else {
            Glide.with(mContext).load(UrlContants.FILE_URL + "resources/watch/" + mData.get(position).getHead()).into(myHolder.contactAvatar);
        }
        myHolder.contactName.setText(mData.get(position).getName());
        myHolder.contactPhone.setText(mData.get(position).getPhone());
        if (onContactClickListener != null) {
            myHolder.parent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = myHolder.getLayoutPosition();
                    onContactClickListener.contactClick(view,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

}
